package XZK010303.dao;

import java.io.IOException;
import java.util.*;

import XZK010303.bean.Express;
import XZK010303.utils.IOUtils;

/**
 * 快递E站-存储快递信息的操作类
 *
 * @author bing.z 2021/5/31 20:40
 **/
public class ExpressDao {
    /**
     * 集合类存储数据，模拟快递柜
     */
    private static Map<String, Express> expressesList;

    static {
        /**
         * 初始化快递柜，从磁盘中获取保存的对象数据
         */
        restoreData();
    }

    private static void restoreData() {
        try {
            expressesList = IOUtils.restoreObj();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (expressesList == null) {
                expressesList = new HashMap<>();
            }
            System.out.println(expressesList.size());
        }
    }

    /**
     * 保存数据至磁盘
     *
     * @throws IOException
     */
    public void storeAll() throws IOException {
        System.out.println(expressesList.size());
        List<Express> list = new ArrayList<>();
        for (String key : expressesList.keySet()) {
            Express express = expressesList.get(key);
            list.add(express);
        }
        IOUtils.storeObj(list);
    }

    /**
     * 获取所有快递信息
     *
     * @return
     */
    public Map<String, Express> getAll() {
        // 从磁盘中获取获取
        restoreData();
        return expressesList;
    }

    /**
     * 新增快递信息
     *
     * @param express
     * @return
     */
    public boolean insert(Express express) throws IOException {
        expressesList.put(express.getNumber(), express);
        // 序列化数据
        storeAll();
        return true;
    }

    public int getSize() {
        return expressesList.size();
    }

    /**
     * 根据快递单号删除快递
     *
     * @param number
     * @return
     */
    public boolean delete(String number) throws IOException {
        if (expressesList == null || expressesList.size() == 0) {
            return false;
        }
        Express express = expressesList.remove(number);
        // 序列化数据
        storeAll();
        return express == null ? false : true;
    }

    /**
     * 根据传入对象的快递单号，更新快递信息
     *
     * @param express
     * @return
     */
    public Express update(Express express) throws IOException {
        expressesList.put(express.getNumber(), express);
        // 序列化数据
        storeAll();
        return expressesList.get(express.getNumber());
    }

    /**
     * 根据取件码获取对象
     *
     * @param code
     * @return
     */
    public Express getByCode(int code) {
        if (expressesList == null || expressesList.size() == 0) {
            return null;
        }
        Set<Map.Entry<String, Express>> entries = expressesList.entrySet();
        for (Map.Entry<String, Express> entry : entries) {
            Express express = entry.getValue();
            if (express.getCode() == code) {
                return express;
            }
        }
        return null;
    }

    /**
     * 根据快递单号获取快递对象
     *
     * @param number
     * @return
     */
    public Express getByNumber(String number) {
        if (expressesList == null || expressesList.size() == 0) {
            return null;
        }
        return expressesList.get(number);
    }
}
